package controll;

import java.io.IOException;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import swimv2.entity.User;
import swimv2.entity.*;
import swimv2.session.IHypertextControllerRemote;
import swimv2.session.ILoginRemote;
import swimv2.session.IWorkModuleRemote;
import utils.SeU;

/**
 * Servlet implementation class Home
 */
public class Home extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Home() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.processRequest(request, response);
	}
	
	private void processRequest(HttpServletRequest request,HttpServletResponse response) 
	{
		String next = "";
		
		HttpSession session = request.getSession();
		
		ILoginRemote log = (ILoginRemote)session.getAttribute(SeU.LOGIN_BEAN_ATTRIBUTE);
		if(log==null)
		{
			session.setAttribute(SeU.STATUS_ATTRIBUTE, "no_user");
			next = "home.jsp";
		}
		else
		{
			try {
				User u = log.getLoggedUser();
				InitialContext ctx = new InitialContext();
				IHypertextControllerRemote ctrl= (IHypertextControllerRemote)ctx.lookup("HypertextController");
				
				List<View> v = ctrl.getDefaultView(u.getId());
				if(v!=null&&v.size()==1)
				{
					session.setAttribute(SeU.STATUS_ATTRIBUTE, "user");
					String name = v.get(0).getName();
					if(name.equals("registered"))
					{
						next = "home.jsp";
						
						
						IWorkModuleRemote wm = (IWorkModuleRemote)ctx.lookup("IWorkModuleRemote");
						
						List<Request> r = wm.getPublicRequest(u.getId());
						
						session.setAttribute(SeU.REQUEST_ATTRIBUTE_ID, r);
						
					}
					if(name.equals("admin"))
					{
						response.sendRedirect("admin_home");
					}
					else
					{
						//next = "home.jsp";
					}
				}
				
				
			} catch (NamingException e) {
				session.setAttribute(SeU.STATUS_ATTRIBUTE, "no_user");
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		//DISPATCHER
		RequestDispatcher dispatcher = request
				.getRequestDispatcher(response.encodeURL(next));
		if (dispatcher!=null)
			try {
				dispatcher.forward(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	

}
